<style lang="less">
    @import '../../styles/loading.less';
</style>

<template>
    <div>
        <Col span="12">
            <Card>
                <p slot="title" style="height:25px;">
                    <Icon type="plus-round"></Icon> {{loop_id?'编辑':'新增'}}轮播图
                </p>
                <Form ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="80">
                    <Form-item label="轮播图" prop="img">
                        <img :src="formValidate.img+'?x-oss-process=image/resize,h_100'" v-if="formValidate.img" style="margin: 10px;">
                        <uploadImg v-on:uploadImg="uploadImg"></uploadImg>
                    </Form-item>
                    <Form-item label="位置" prop="position">
                        <Radio-group v-model="formValidate.position">
                            <Radio label="1">商城首页</Radio>
                        </Radio-group>
                    </Form-item>
                    <Form-item label="跳转链接" prop="redirect">
                        <Input v-model="formValidate.redirect" placeholder="Enter..."></Input>
                    </Form-item>
                    <Form-item label="简介" prop="abstract">
                        <Input type="textarea" v-model="formValidate.abstract" placeholder="Enter..."></Input>
                    </Form-item>
                    <Form-item label="排序" prop="sort">
                        <Input v-model="formValidate.sort" placeholder="Enter..."></Input>
                    </Form-item>

                    <Form-item>
                        <Button type="primary" @click="handleSubmit('formValidate')">提交</Button>
                        <Button type="ghost" v-if="!loop_id" @click="handleReset('formValidate')" style="margin-left: 8px">重置</Button>
                    </Form-item>
                </Form>
            </Card>
        </Col>
    </div>
</template>

<script>
    import uploadImg from '../website/upload';
    export default {
        components:{
            'uploadImg':uploadImg,
        },
        data () {
            return {
                loop_id: '',
                formValidate: {
                    img: '',
                    position: 1,
                    redirect: '',
                    abstract: '',
                    sort: 1,
                },
                ruleValidate: {
                    img: [
                        { required: true, message: '请上传图片' },
                    ],
                    position: [
                        { required: true, message: '请选择位置' },
                    ],
                    abstract: [
                        { required: true, message: '请输入简介' },
                    ],
                    sort: [
                        { required: true, message: '请输入排序值' },
                    ],
                },
            }
        },
        methods: {
            handleSubmit (name) {
                this.$refs[name].validate((valid) => {
                    if (valid) {
                        let url = this.loop_id ? 'admin/loopImg/edit' : 'admin/loopImg/add';
                        let postData = this.formValidate;
                        postData.loop_id = this.loop_id; console.log(postData);
                        this.$http.post(url, this.$qs.stringify(postData)).then((res) => {
                            var d = res.data;
                            if (d.code == 0) {
                                this.$Notice.success({
                                    title: 'Success',
                                    desc: '操作成功'
                                });
                                this.$router.push({
                                    name: 'loopImg'
                                });
                            } else {
                                this.$Notice.error({
                                    title: 'Error Warning',
                                    desc: `Error：${d.msg}<br>`
                                });
                            }
                        });
                    } else {
                        this.$Message.error('表单验证失败!');
                    }
                })
            },
            handleReset (name) {
                this.$refs[name].resetFields();
            },
            uploadImg (data) {
                console.log(data);
                this.formValidate.cover_img = data;
            },
            getOne () {
                this.$http.post('admin/loopImg/getOne', this.$qs.stringify({loop_id: this.loop_id})).then((res) => {
                    var d = res.data;
                    if (d.code == 0) {
                        this.formValidate = {
                            img: d.data.img,
                            position: d.data.position,
                            redirect: d.data.redirect,
                            abstract: d.data.abstract,
                            sort: d.data.sort,
                        };
                    } else {
                        this.$Notice.error({
                            title: 'Error Warning',
                            desc: `Error：${d.msg}<br>`
                        });
                    }
                });
            },
        },
        mounted() {
            this.$nextTick(() => {
                this.loop_id = this.$route.query.loop_id;
                if (this.loop_id) {
                    this.getOne();
                }
            });
        },
    };
</script>
